package com.example.service;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.example.common.Result;
import com.example.common.enums.LogsTypeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Pages;
import com.example.entity.Question;
import com.example.entity.QuestionItem;
import com.example.mapper.PagesMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.swing.*;
import java.beans.Transient;
import java.util.List;

//问卷表业务处理
@Service
public class PagesService {

    @Resource
    private PagesMapper pagesMapper;

    @Resource
    QuestionService questionService;

    @Resource
    QuestionItemService questionItemService;

//    新增 创建人ID、创建时间
    public void add(Pages pages) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(currentUser.getRole().equals(RoleEnum.USER.name())){
            pages.setUserId(currentUser.getId());
        }
        pages.setCreateTime(DateUtil.now());
        pagesMapper.insert(pages);
        //记录日志
        LogsService.recordLog("创建问卷【"+pages.getName()+"】", LogsTypeEnum.ADD.getValue(),currentUser.getUsername());
    }

//    删除
    @Transactional
    public  void deleteById(Integer id) {
        Account currentUser=TokenUtils.getCurrentUser();
        Pages pages=this.selectById(id);
        pagesMapper.deleteById(id);
        //记录日志
        LogsService.recordLog("删除问卷【"+pages.getName()+"】", LogsTypeEnum.DELETE.getValue(),currentUser.getUsername());
        questionService.deleteByPageId(id);
    }

//    批量删除
    public void deleteBatch(List<Integer> ids){
        for (Integer id : ids){
            this.deleteById(id);
        }
    }

//    修改
    public void updateById(Pages pages){
        Account currentUser=TokenUtils.getCurrentUser();

        pagesMapper.updateById(pages);
        LogsService.recordLog("修改问卷【"+pages.getName()+"】", LogsTypeEnum.UPDATE.getValue(),currentUser.getUsername());
    }

//    根据ID查询
    public Pages selectById(Integer id){
        return pagesMapper.selectById(id);
    }

//    查询所有
    public List<Pages> selectAll(Pages pages) {
        return pagesMapper.selectAll(pages);
    }

//    分页查询
    public PageInfo<Pages> selectPages(Pages pages, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            //如果是用户，需要筛选自己的问卷信息
            pages.setUserId((currentUser.getId()));
        }
//        Spring Boot中的Mybatis分页插件-pagehelper
        PageHelper.startPage(pageNum, pageSize);
        List<Pages> list = pagesMapper.selectAll(pages);
        return PageInfo.of(list);
    }
@Transactional
    public Integer copy(Integer pageId) {
        Pages pages = this.selectById(pageId);//拿到当前被复制的调查问卷
        pages.setCount(pages.getCount()+1);
        this.updateById(pages);//更新使用次数

        Pages newPage = new Pages();
        newPage.setName(pages.getName() + "--COPY");
        //Account currentUser = TokenUtils.getCurrentUser();//拿到当前用户信息
        //newPage.setId(currentUser.getId());
        newPage.setDescr(pages.getDescr());
        newPage.setImg(pages.getImg());
        this.add(newPage);
        List<Question> questionList = questionService.selectByPageId(pageId);//查询所有题目信息
        for(Question question: questionList){
            //Question newQuestion = new Question();
            //newQuestion.setPageId(question.getPageId());
            question.setId(null);//删除之前的主键，插入新数据
            question.setPageId(newPage.getId());
            questionService.add(question);
            List<QuestionItem> questionItemList = question.getQuestionItemList();
            for(QuestionItem questionItem:questionItemList){
                questionItem.setId(null);
                questionItem.setQuestionId(question.getId());
                questionItemService.add(questionItem);
            }
        }
        LogsService.recordLog("复制新的问卷【"+newPage.getName()+"]",LogsTypeEnum.COPY.getValue(),TokenUtils.getCurrentUser().getUsername());
       return newPage.getId();
    }
}
